package com.stx.jeepay.manager.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stx.jeepay.core.aop.MethodLog;
import com.stx.jeepay.core.entity.SysEntitlementEntity;
import com.stx.jeepay.core.model.ApiRes;
import com.stx.jeepay.core.service.SysEntitlementService;
import com.stx.jeepay.core.utils.TreeDataBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统管理（用户权限）")
@RestController
@RequestMapping("api/sysEnts")
public class SysEntController extends CommonCtrl {

    @Autowired
    SysEntitlementService sysEntitlementService;

    @ApiOperation("查询菜单权限详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "entId", value = "权限ID[ENT_功能模块_子模块_操作], eg: ENT_ROLE_LIST_ADD", required = true),
            @ApiImplicitParam(name = "sysType", value = "所属系统： MGR-运营平台, MCH-商户中心", required = true)
    })
    @PreAuthorize("hasAnyAuthority( 'ENT_UR_ROLE_ENT_LIST' )")
    @GetMapping("/bySysType")
    public ApiRes bySystem() {
        return ApiRes.ok(sysEntitlementService.getOne(new LambdaQueryWrapper<SysEntitlementEntity>()
                .eq(SysEntitlementEntity::getEntId, getValStringRequired("entId"))
                .eq(SysEntitlementEntity::getSysType, getValStringRequired("sysType"))));
    }

    @ApiOperation("更新权限资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "entId", value = "权限ID[ENT_功能模块_子模块_操作], eg: ENT_ROLE_LIST_ADD", required = true),
            @ApiImplicitParam(name = "entName", value = "权限名称", required = true),
            @ApiImplicitParam(name = "menuUri", value = "菜单uri/路由地址"),
            @ApiImplicitParam(name = "entSort", value = "排序字段, 规则：正序"),
            @ApiImplicitParam(name = "quickJump", value = "快速开始菜单 0-否, 1-是"),
            @ApiImplicitParam(name = "state", value = "状态 0-停用, 1-启用")
    })
    @PreAuthorize("hasAuthority( 'ENT_UR_ROLE_ENT_EDIT')")
    @MethodLog(remark = "更新资源权限")
    @PutMapping("/{entId}")
    public ApiRes updateById(@PathVariable("entId") String entId) {
        SysEntitlementEntity queryObject = getObject(SysEntitlementEntity.class);
        sysEntitlementService.update(new LambdaQueryWrapper<SysEntitlementEntity>()
                .eq(SysEntitlementEntity::getEntId,entId)
                .eq(SysEntitlementEntity::getSysType,queryObject.getSysType()));
        return ApiRes.ok();
    }


    @ApiOperation("查询权限集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "sysType", value = "所属系统： MGR-运营平台, MCH-商户中心", required = true)
    })
    @GetMapping("/showTree")
    public ApiRes<List<JSONObject>> showTree() {
        List<SysEntitlementEntity> list = sysEntitlementService.list(new LambdaQueryWrapper<SysEntitlementEntity>()
                .eq(SysEntitlementEntity::getSysType, getValStringRequired("sysType")));
        //转换为json树状结构
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(list);
        List<JSONObject> leftMenuTree = new TreeDataBuilder(jsonArray,
                "entId", "pid", "children", "entSort", true)
                .buildTreeObject();
        return ApiRes.ok(leftMenuTree);
    }
}
